//
//  ReservationService.m
//  homeland
//
//  Created by SunShine on 13-10-29.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ReservationService.h"
#import "UserInfoService.h"

@implementation ReservationService

-(void)requestDepartList
{
    if (self.dataList) {
        self.dataList = nil;
    }
    _dataItemName = @"ReservationDepartListItem";
    [self doHttpGet:RESERVATION_DEPART_LIST_URL headers:nil path:nil parameters:nil];
}

-(void)requestReservationListForCode:(NSString *)code AndSearchkey:(NSString *)key
{
    _currentPageNo = 0;
    if (self.dataList) {
        self.dataList = nil;
    }
    [self requestMoreReservationListForCode:code AndSearchkey:key];
}
-(void)requestMoreReservationListForCode:(NSString *)code AndSearchkey:(NSString *)key
{
    _dataItemName =@"ReservationListItem";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (code) {
        [params setValue:code forKey:@"unitCode"];
    }
    if (key) {
        [params setValue:key forKey:@"showName"];
    }
    [self doHttpPost:RESERVATION_LIST_URL headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

-(void)commitReservationWithName:(NSString *)name Phone:(NSString *)phone CardNo:(NSString *)cardNo Date:(NSString *)date BeginTime:(NSString *)begin EndTime:(NSString *)end ListItem:(ReservationListItem *)listItem
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"0" forKey:@"reserveCode"];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    }
    else
    {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }
    [params setValue:[[UIDevice currentDevice].identifierForVendor UUIDString] forKey:@"userImsiCode"];
    [params setValue:phone forKey:@"mobileNum"];
    [params setValue:name forKey:@"name"];
    [params setValue:cardNo forKey:@"idcardNum"];
    [params setValue:date forKey:@"reserveDate"];
    [params setValue:begin forKey:@"reserveStime"];
    [params setValue:end forKey:@"reserveEtime"];
    [params setValue:@"0" forKey:@"isFromInterface"];
    [params setValue:listItem.businessCode forKey:@"businessCode"];
    [params setValue:listItem.businessName forKey:@"businessName"];
    [params setValue:listItem.unitCode forKey:@"unitCode"];
    [params setValue:listItem.unitName forKey:@"unitName"];
    [params setValue:getDeviceIdentifier() forKey:@"imsi_code"];//要加入的唯一标示
    [params setValue:@"0" forKey:@"policyType"];
    [self doCommit:RESERVATION_COMMIT_URL headers:nil path:nil parameters:params];
}

-(void)doCommit:(NSString *)posturl headers:(NSDictionary*)headersOrNil path:(NSString*)path parameters:(NSDictionary *)params
{
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:path params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        //转换对象,添加到列表中
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}

-(void)getReservationRecordListForCode:(NSString *)code Name:(NSString *)name
{
    _dataItemName = @"ReservationRecordListItem";
    _currentPageNo = 0;
    if (self.dataList) {
        self.dataList = nil;
    }
    [self getMoreReservationRecordListForCode:code Name:name];
}

-(void)getMoreReservationRecordListForCode:(NSString *)code Name:(NSString *)name
{
    _dataItemName = @"ReservationRecordListItem";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (code && code.length > 0) {
        [params setValue:code forKey:@"reserveCode"];
    }
    if (name && name.length > 0) {
        [params setValue:name forKey:@"name"];
    }
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    }
    if ([params count]==0) {
        //没有任何参数，返回空值
        SuccessBlock(nil);
        return;
    }
    [self doHttpPost:RESERVATION_RECORD_LIST_URL headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

-(void)voteForReservationWithID:(NSString *)ID AndVote:(NSInteger)vote
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSString stringWithFormat:@"%d",ID.integerValue] forKey:@"id"];
    [params setValue:[NSString stringWithFormat:@"%d",vote] forKey:@"voteGrade"];
    [self doHttpPost:RESERVATION_RECORD_VOTE_URL headers:nil path:nil parameters:params];
}
@end
